package com.lge.clock.alarmclock;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lge.clock.R;
import com.lge.clock.alarmclock.StopSnoozeButton.StopListener;
import com.lge.clock.util.Log;

/**
 * Puzzle Class used in Puzzle Lock
 * @author LGE Cappuccino
 */
public class GridPuzzle extends LinearLayout implements OnTouchListener {
	private static final String TAG = "AlarmAlert";
	private GridPuzzle myself;

	private Button mButtonLT;
	private Button mButtonRT;
	private Button mButtonLB;
	private Button mButtonRB;
	
	/**
	 * Field mNumberOrders.
	 */
	int[] mNumberOrders = {R.string.sp_puzzle_num_1_NORMAL, R.string.sp_puzzle_num_2_NORMAL, R.string.puzzle_num_3, R.string.puzzle_num_4, R.string.puzzle_num_5
			, R.string.puzzle_num_6, R.string.puzzle_num_7, R.string.puzzle_num_8, R.string.puzzle_num_9, R.string.puzzle_num_10};
	
	/**
	 * Field mAlphabetOrders.
	 */
	int[] mAlphabetOrders = {R.string.activity_a_label, R.string.activity_b_label, R.string.activity_c_label, R.string.activity_d_label, R.string.activity_e_label
			, R.string.activity_f_label, R.string.activity_g_label, R.string.activity_h_label, R.string.activity_i_label, R.string.activity_j_label};
	
	int[] text_other= {R.string.puzzle_ot_1, R.string.puzzle_ot_2, R.string.puzzle_ot_3, R.string.puzzle_ot_4, R.string.puzzle_ot_5
			, R.string.puzzle_ot_6, R.string.puzzle_ot_7, R.string.puzzle_ot_8, R.string.puzzle_ot_9, R.string.puzzle_ot_10};
	
	private static final int BTN_CNT = 4;
	private static final int SUCCESS_CNT = BTN_CNT;
	private static final int LT_IDX = 0, RT_IDX = 1, LB_IDX = 2, RB_IDX = 3;
	private static final int PUZZLE_RANGE = 9;
	
	/**
	 * Field mPuzzleCnts.
	 */
	int[] mPuzzleCnts = new int[BTN_CNT];
	/**
	 * Field mCompareCnts.
	 */
	int[] mCompareCnts = new int[BTN_CNT];
	
	private int count = 0;
	private int mLT = 0;
	private int mRT = 0;
	private int mLB = 0;
	private int mRB = 0;
	
	private String mPuzzleSetting_Value = null;
	
	private static final String PREFERENCES = "com.lge.clock_preferences";
	
	/**
	 * Constructor for GridPuzzle.
	 * @param context Context
	 */
	public GridPuzzle(Context context) {
		this(context, null);
	}  
	/**
	 * Constructor for GridPuzzle.
	 * @param context Context
	 * @param attrs AttributeSet
	 */
	public GridPuzzle(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}           
	
	/**
	 * Constructor for GridPuzzle.
	 * @param context Context
	 * @param attrs AttributeSet
	 * @param defStyle int
	 */
	public GridPuzzle(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		// [start][yeonju7.kim][IN][SDQ_11][P940][41494] 2011.08.19
		count = 0;
		// [end][yeonju7.kim][IN][SDQ_11][P940][41494] 2011.08.19
		LayoutInflater inflater = (LayoutInflater) context.getSystemService("layout_inflater");
		myself = (GridPuzzle)inflater.inflate(R.layout.alarm_grid_puzzle, this, true);
		
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES, 0);
		mPuzzleSetting_Value = pref.getString("puzzle_setting", "0");
		
		Log.i(TAG, "PuzzleSetting_Value : " + mPuzzleSetting_Value);
		
		mButtonLT = (Button) findViewById(R.id.leftTopButton);
		mButtonRT = (Button) findViewById(R.id.rightTopButton);
		mButtonLB = (Button) findViewById(R.id.leftBottomButton);
		mButtonRB = (Button) findViewById(R.id.rightBottomButton);
		
		SecureRandom ran = new SecureRandom();   //Random ran = new Random();
		
		int textValue = 0;
		int overlapTest = 0;
		
		for(int index = 0; index < 4; index++){
			
			textValue = ran.nextInt(PUZZLE_RANGE);
			
			for(int overlap = 0; overlap <= index; overlap++) {
					
				if(mPuzzleCnts[overlap] == textValue) {
					index = index - 1;
					overlapTest = 1;
				}
			}
				
			if(overlapTest != 1) {
				mPuzzleCnts[index] = textValue;
				mCompareCnts[index] = mPuzzleCnts[index];
			} 
			else {
				overlapTest = 0;
			}
		}
		
		if(mPuzzleSetting_Value.equals("0")) {
			mButtonLT.setText(mNumberOrders[mPuzzleCnts[0]]);
			mButtonRT.setText(mNumberOrders[mPuzzleCnts[1]]);
			mButtonLB.setText(mNumberOrders[mPuzzleCnts[2]]);
			mButtonRB.setText(mNumberOrders[mPuzzleCnts[3]]);
		}
		else if(mPuzzleSetting_Value.equals("1")) {
			mButtonLT.setText(mAlphabetOrders[mPuzzleCnts[0]]);
			mButtonRT.setText(mAlphabetOrders[mPuzzleCnts[1]]);
			mButtonLB.setText(mAlphabetOrders[mPuzzleCnts[2]]);
			mButtonRB.setText(mAlphabetOrders[mPuzzleCnts[3]]);
		}
		
		else if(mPuzzleSetting_Value.equals("2"))
		{
			mButtonLT.setText(text_other[mPuzzleCnts[0]]);
			mButtonRT.setText(text_other[mPuzzleCnts[1]]);
			mButtonLB.setText(text_other[mPuzzleCnts[2]]);
			mButtonRB.setText(text_other[mPuzzleCnts[3]]);
		}
		
		
		Arrays.sort(mCompareCnts);
		for(int index = 0; index < 4; index++) {
			Log.i(TAG, "mCompareCnts[" + index + "] : " + mCompareCnts[index]);
		}
		
		mButtonLT.setOnTouchListener(this);
		mButtonRT.setOnTouchListener(this);
		mButtonLB.setOnTouchListener(this);
		mButtonRB.setOnTouchListener(this);
	}
	
	/**
	 * Method onTouch.
	 * @param v View
	 * @param event MotionEvent
	 * @return boolean
	 * @see android.view.View$OnTouchListener#onTouch(View, MotionEvent)
	 */
	public boolean onTouch(View v, MotionEvent event) {
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(mLT != 0) {
					mButtonLT.setBackgroundResource(R.drawable.btn_bg_green_n);
					mLT = 0;
				}
				else if(mRT != 0) {
					mButtonRT.setBackgroundResource(R.drawable.btn_bg_blue_n);
					mRT = 0;
				}
				else if(mLB != 0) {
					mButtonLB.setBackgroundResource(R.drawable.btn_bg_yellow_n);
					mLB = 0;
				}
				else if(mRB != 0) {
					mButtonRB.setBackgroundResource(R.drawable.btn_bg_violet_n);
					mRB = 0;
				}
			
				if( v.equals(mButtonLT) ) { // mPuzzleCnts[0]
					if(mLT == 0) {
						if( equalLegalNumber(count++ , LT_IDX) ) 
						{
							mButtonLT.setBackgroundResource(R.drawable.btn_bg_green_p);
							mButtonLT.setEnabled(false);
							if(count == SUCCESS_CNT) { // successfully
								count = 0;
								stopGridChanged();
							}
						}
						else {
							mButtonLT.setBackgroundResource(R.drawable.btn_bg_red_p_l);
							count--;
							mLT++;
						}
					}
				}
				else if( v.equals(mButtonRT) ) { //mPuzzleCnts[1]
					if(mRT == 0) {
						if( equalLegalNumber(count++ , RT_IDX) ) 
						{
							mButtonRT.setBackgroundResource(R.drawable.btn_bg_blue_p);
							mButtonRT.setEnabled(false);
							if(count == SUCCESS_CNT) { // successfully
								count = 0;
								stopGridChanged();
							}
						}
						else {
							mButtonRT.setBackgroundResource(R.drawable.btn_bg_red_p_r);
							count--;
							mRT++;
						}
					}
				}
				else if( v.equals(mButtonLB) ) { //mPuzzleCnts[2] 
					if(mLB == 0) {
						if( equalLegalNumber(count++ , LB_IDX) ) 
						{
							mButtonLB.setBackgroundResource(R.drawable.btn_bg_yellow_p);
							mButtonLB.setEnabled(false);
							if(count == SUCCESS_CNT) { // successfully
								count = 0;
								stopGridChanged();
							}
						}
						else {
							mButtonLB.setBackgroundResource(R.drawable.btn_bg_red_p_l);
							count--;
							mLB++;
						}
					}
				}
				else if( v.equals(mButtonRB) ) {	//mPuzzleCnts[3]
					if(mRB == 0) {
						if( equalLegalNumber(count++ , RB_IDX) ) {
							mButtonRB.setBackgroundResource(R.drawable.btn_bg_violet_p);
							mButtonRB.setEnabled(false);
							if(count == SUCCESS_CNT) { // successfully
								count = 0;
								stopGridChanged();
							}
						}
						else {
							mButtonRB.setBackgroundResource(R.drawable.btn_bg_red_p_r);
							count--;
							mRB++;
						}
					}
				}
				break;
				
			case MotionEvent.ACTION_UP:
				break;
				
			default :
				break;
		}
		return false;
	}
	
	private boolean equalLegalNumber(int compare, int puzzle) {
		if( mCompareCnts[compare] == mPuzzleCnts[puzzle] )
			return true;
		else
			return false;
	}
	
	private StopListener mStopGridListener;	
	
	/**
	 * StopGridListener
	 * @author sungtan.back
	 */
	public interface StopGridListener{
		/**
		 * Method stopGridChanged.
		 */
		void stopGridChanged();
	}	 
	/**
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==mStopButton)
		{
			stopGridChanged();
		}
		else if(v==mSnoozeButton)
		{
			snoozeGridChanged();
		}
	}
	*/
	public void setStopGridListener(StopListener aa) {
        mStopGridListener = aa ;
    }    
    
    private void stopGridChanged() {
    	mStopGridListener.stopChanged(); //
	}
}